package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class BibliotecaApp {


    private Collection<Movie> movieList;
    private BufferedReader reader;
    private PrintStream printStream;
    private Map<String, Integer> bookList;
    private Map<Integer, MenuOptions> menuMap;
    private Map<String,UserAccount> userMap;
    private CurrentUser currentUser;

    public BibliotecaApp(BufferedReader reader, PrintStream ps, Map<String, Integer> bookList, Collection<Movie> movieList, Map<String, UserAccount> userMap, CurrentUser currentUser) {
        this.reader = reader;
        this.printStream = ps;
        this.bookList = bookList;
        this.movieList = movieList;
        this.userMap = userMap;
        this.currentUser = currentUser;
    }

    public static void main(String[] args) throws IOException {
        CurrentUser currentUser = new CurrentUser();
        Loader loader = new Loader();
        BibliotecaApp bibliotecaApp = new BibliotecaApp(new BufferedReader(new InputStreamReader(System.in)),
                System.out, loader.loadBooks(), loader.loadMovies(), loader.loadUsers(), currentUser);

        bibliotecaApp.startApp();

    }

    public void startApp() throws IOException {

        menuMap = new HashMap<Integer, MenuOptions>();

        populateMenuMap();

        MainMenu mainMenu = new MainMenu(reader, printStream, bookList, movieList, menuMap);

        mainMenu.execute();

    }

    private void populateMenuMap() {
        menuMap.put(1, new PrintBooksMenu(printStream, bookList));
        menuMap.put(2, new ReserveBooksMenu(printStream, reader, bookList, currentUser));
        menuMap.put(3, new DisplayMembershipDetailsMenu(printStream, currentUser));
        menuMap.put(4, new MovieMenu(printStream, movieList));
        menuMap.put(5, new LoginMenu(printStream, reader, userMap, currentUser));
    }
}
